package com.wonder.app.controller;

import cn.hutool.json.JSONObject;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.service.StkTransferAppService;
import com.wonder.common.common.AjaxResult;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "调拨申请单")
@RestController
@RequestMapping("/app/StkTransferapp")
public class StkTransferAppController {

    @Autowired
    private StkTransferAppService stkTransferAppService;

    @Autowired
    private SysAdminService adminService;

    @Autowired
    private CheckAdminToken checkAdminToken;
    /**
     * @explain 金蝶审核调拨申请单
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/audit")
    @ApiOperation(value = "审核", notes = "作者：Wonder")
    public AjaxResult<Boolean> audit(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkTransferAppService.audit(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    @ApiOperation(value = "调拨申请单列表(手机端)")
    @RequestMapping(value = "/get_stk_transfer_app_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getStkTransferAppList(@RequestBody Map<String,Object> map) {
        Map<String,Object> data = stkTransferAppService.getStkTransferAppList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    @ApiOperation(value = "调拨申请单详情(手机端)")
    @RequestMapping(value = "/get_stk_transfer_app_detail", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getStkTransferAppDetail(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Map<String,Object> data = stkTransferAppService.getStkTransferAppDetail(user_id,map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    @ApiOperation(value = "修改调拨申请详情谁(手机端)")
    @RequestMapping(value = "/update_stk_transfer_app_detail", method = RequestMethod.POST)
    public AjaxResult<?> updateStkTransferAppDetail(@RequestBody Map<String,Object> map) throws Exception{
        Boolean res = stkTransferAppService.updateStkTransferAppDetail(map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    @ApiOperation(value = "审核调拨申请单(手机端)")
    @RequestMapping(value = "/audit_stk_transfer_app", method = RequestMethod.POST)
    public AjaxResult<?> auditStkTransferApp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean data = stkTransferAppService.auditStkTransferApp(user_id,userName,map);
        if (data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    @ApiOperation(value = "审核调拨申请单(包材)")
    @RequestMapping(value = "/audit_stk_transfer_app_bao_cai", method = RequestMethod.POST)
    public AjaxResult<?> auditStkTransferAppBaoCai(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean data = stkTransferAppService.auditStkTransferAppBaoCai(user_id,userName,map);
        if (data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 调拨申请关闭
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/stk_apply_close")
    @ApiOperation(value = "调拨申请整单关闭", notes = "作者：Wonder")
    public AjaxResult<Boolean> receiveClose(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkTransferAppService.applyClose(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * 调拨申请反审核
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/unaudit")
    @ApiOperation(value = "审核", notes = "作者：Wonder")
    public AjaxResult<Boolean> unaudit(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkTransferAppService.unAudit(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

}
